package com.example.date;

import org.junit.Test;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;

/**
 * @author readpage
 * @date 2023-01-29 14:55
 */
public class LocalDateTest {

    //public static Xxxxx now(): 获取系统当前时间对应的该对象
    @Test
    public void test() {
        // 1.获取本地日期时间(不可变对象)
        LocalDate ld = LocalDate.now();  // 年 月 日
        System.out.println(ld);

        // 获取日期对象中的信息
        int year = ld.getYear(); // 年
        int month = ld.getMonthValue(); // 月(1-12)
        int day = ld.getDayOfMonth(); // 日
        int dayOfYear = ld.getDayOfYear();  // 一年中的第几天
        int dayOfWeek = ld.getDayOfWeek().getValue(); // 星期几
        System.out.printf("%d年\t%d月\t%d日\t一年第%d天\t星期%d\n", year, month, day, dayOfYear, dayOfWeek);
        System.out.println("**********");
        System.out.println(ld.with(TemporalAdjusters.firstDayOfMonth())); // 获取当前月的第一天
        System.out.println(ld.with(TemporalAdjusters.lastDayOfMonth())); // 获取当前月的最后一天
    }

    @Test
    public void test2() {
        LocalDate ld = LocalDate.now();
        // 2.直接修改某个信息: 年:withYear、月:withMonth、日:withDayOfMonth、今年第几天:withDayOfYear
        LocalDate ld2 = ld.withYear(2099);
        LocalDate ld3 = ld.withYear(2099).withMonth(12).withDayOfMonth(2);
        System.out.println("ld:" + ld);
        System.out.println("ld2:" + ld2);
        System.out.println("ld3:" + ld3);
    }

    @Test
    public void test3() {
        LocalDate ld = LocalDate.now();
        // 3.把某个信息加多少: plusYears、plusMonths、plusDays、plusWeeks
        System.out.println(ld);
        System.out.println(ld.plusYears(2));
        System.out.println(ld.plusMonths(2));
        System.out.println(LocalDate.of(2023, 1, 31).plusDays(3));
        System.out.println("********************");
        // 4.把某个信息减多少: minusYears、minusMonths、minusDays、minusWeeks
        System.out.println(ld.minusYears(2));
        System.out.println(ld.minusMonths(2));
        System.out.println(ld.minusDays(30));
    }

    @Test
    public void test4() {
        System.out.println(LocalDate.parse("2022-10-10"));
        // 5.判断2个日期的LocalDate对象: public static LocalDate of(int year,, int month)
        LocalDate ld = LocalDate.now();
        LocalDate ld2 = LocalDate.of(2099, 12, 12);
        LocalDate ld3 = LocalDate.of(2099, 12, 12);
        System.out.println(ld2);
        System.out.println("*******************");
        // 6.判断2个日期对象，是否相等，在前还是在后: equals isBefore isAfter
        System.out.println(ld2.equals(ld3)); //true
        System.out.println(ld2.isAfter(ld)); //true
        System.out.println(ld2.isBefore(ld)); //false
    }

    @Test
    public void test5() {
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println(ldt);
        // 7. 可以把LocalDateTime转换成LocalDate和LocalTime
        // public LocalDate toLocalDate()
        // public LocalTIme toLocalTime()
        // public static LocalDateTime of(LocalDate date, LocalTime time)
        LocalDate ld = ldt.toLocalDate();
        LocalTime lt = ldt.toLocalTime();
        System.out.println(ld);
        System.out.println(lt);

        LocalDateTime ldt2 = LocalDateTime.of(ld, lt);
        System.out.println(ldt2);

        //LocalDate 转 LocalDateTime
        // atStartOfDay() 此日期开始时的午夜的本地日期时间，不为null。
        System.out.println(ld.atStartOfDay());

    }

    @Test
    public void test6() {
        // until 返回此LocalDate和结束LocalDate之间的时间
        LocalDate now = LocalDate.now();
        LocalDate end = now.plusMonths(1);
        // 计算两个时间的天数差
        long until = now.until(end, ChronoUnit.DAYS);
        System.out.println(end.toEpochDay() - now.toEpochDay());
        System.out.println(until);
        // 本月最后天一天的日期
        LocalDate with = now.with(TemporalAdjusters.lastDayOfMonth());
        System.out.println(with);
    }

    @Test
    public void test7() {
        LocalDate start = LocalDate.parse("2023-02-01");
        LocalDate end = start.withDayOfMonth(15);
        LocalDate end2 = end.withMonth(3);
        // 计算两个时间的周数差
        long until = start.until(end, ChronoUnit.WEEKS);
        System.out.println(until);
        // 计算两个时间的月数差
        long until2 = start.until(end2, ChronoUnit.MONTHS);
        System.out.println(until2);
    }

    @Test
    public void test8() {
        LocalDate now = LocalDate.now();
        // 获得当前日期的所在周的周一(previousOrSame:如果当前日期是周一，就返回当前日期;否则就返回上一个周一。)
        System.out.println("上一个周一或本周一：" + now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)));
        System.out.println("下一个周日或本周日：" + now.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)));
        LocalDate parse = LocalDate.parse("2023-02-01");
        System.out.println(parse.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)));
        System.out.println("-------获取一个月的天数--------");
        System.out.println(parse.lengthOfMonth());
    }
}
